﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OilRigSumoWrestlers.Controllables.Controllers;
using OilRigSumoWrestlers.Entities.MenuItems;
using OilRigSumoWrestlers.Scenes.Menu;
using RaptorEngine;
using RaptorEngine.Components.Drawable;

namespace OilRigSumoWrestlers.Scenes
{
    class WinScene:MenuScene
    {
        private string _playerName;
        public WinScene(string playerName) : base(false)
        {
            _playerName = playerName;

            CreateScene();
            CreateController();
            GameEngine.Instance.Audio.StopMusic();
            GameEngine.Instance.Audio.PlayMusic("win", false);
        }

        private void CreateController()
        {
            Entity pauseController = new Entity();
            pauseController.AddComponent(new MenuControllable(pauseController, this, RaptorEngine.Managers.LogicalPlayerIndex.One));
            AddEntity(pauseController);
        }

        private void CreateScene()
        {
            Entity bgImage = new Entity();
            bgImage.AddComponent(new DrawableSprite(bgImage, GameEngine.Instance.Load<Texture2D>("Images/black"), Color.Black, 0.6f));
            bgImage.Scale = GameEngine.Instance.Render.GraphicsDevice.Viewport.Width;
            AddEntity(bgImage);

            Vector2 worldCenter = new Vector2(
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.X + GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Width / 2,
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Y + GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Height / 2);

            //Create Text
            string winText = _playerName+" has won!";

            Entity winMenu = new Entity(worldCenter + new Vector2(0, -20));
            winMenu.AddComponent(new DrawableText(winMenu,
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuFont"), winText, Color.White, 1, true));

            Entity winMenuShadow = new Entity(worldCenter + new Vector2(0, -20) + Vector2.One);
            winMenuShadow.AddComponent(new DrawableText(winMenuShadow,
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuFont"), winText, Color.Black, 1, true));

            //Add texts to the scene
            AddEntity(winMenuShadow);
            AddEntity(winMenu);

            //Exit to main menu text
            string exitToMainMenuText = "Exit to menu";

            DrawableText optExitToMainMenuSelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 80)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitToMainMenuText, Color.White, 1, true);

            DrawableText optExitToMainMenu = new DrawableText(new Entity(worldCenter + new Vector2(0, 80)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitToMainMenuText, Color.Gray, 1, true);

            //Exit Game text
            string exitGameText = "Exit Game";

            DrawableText optExitGameSelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 120)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitGameText, Color.White, 1, true);

            DrawableText optExitGame = new DrawableText(new Entity(worldCenter + new Vector2(0, 120)),
                GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), exitGameText, Color.Gray, 1, true);

            _menuItems.Add(new ExitToMenuItem(optExitToMainMenuSelected, optExitToMainMenu));
            _menuItems.Add(new ExitGameItem(optExitGameSelected, optExitGame));
            _menuItems[_selectedItem].Select();

            foreach (MenuItem item in _menuItems)
            {
                AddEntity(item);
            }
        }


        public override void GoBack()
        {
            GameEngine.Instance.Scene.RemoveCurrentScene();
            GameEngine.Instance.Scene.RemoveCurrentScene();
            GameEngine.Instance.Scene.StartCurrentScene();
            GameEngine.Instance.Scene.ShowCurrentScene();
        }

    }
}
